//==========================================================
// 此代码由TilverNET代码生成工具自动生成，请不要修改
// 代码生成器版本号：1.2
// 作者：landy
// 联系方式：(QQ)86566178
// 创建时间：2014/6/21 12:29:06
//==========================================================

using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using Tilver.Data;
using Tilver.Data.DataPager;

namespace Weixin.MP.DAL.WeixinMP
{
	#region EO

	/// <summary>
	/// 关注用户信息 《dbo.AppUserInfo(实体类)》
	/// </summary>
	[Serializable]
	public class AppUserInfoEO : IRowMapper<AppUserInfoEO>
	{
		protected bool _hasOriginal = false;
		protected string _openid;
		protected string _originalOpenid;
		protected string _nickname;
		protected int? _sex;
		protected string _language;
		protected string _city;
		protected string _province;
		protected string _country;
		protected string _headimgurl;
		protected int _subscribe;
		protected long? _subscribeTime;
		protected DateTime _createtime;

		/// <summary>
		/// 主键是否有原始值（即数据库中保存的值），用于实体对象更新时
		/// </summary>
		public bool HasOriginal
		{
			get { return _hasOriginal; }
		}


		/// <summary>
		/// 普通用户的标识，对当前公众号唯一 《[主键]  [varchar]》
		/// </summary>
		public string Openid
		{
			get { return _openid; }
			set { _openid = value;}
		}

		/// <summary>
		/// 普通用户的标识，对当前公众号唯一 《数据库中的原始主键值副本》
		/// </summary>
		public string OriginalOpenid
		{
			get { return _originalOpenid; }
			set { 
				_hasOriginal = true;
				_originalOpenid = value;
			}
		}

		/// <summary>
		/// 用户的昵称 《[字段]  [varchar]》
		/// </summary>
		public string Nickname
		{
			get { return _nickname; }
			set { _nickname = value;}
		}

		/// <summary>
		/// 用户的性别，值为1时是男性，值为2时是女性，值为0时是未知 《[字段]  [int]》
		/// </summary>
		public int? Sex
		{
			get { return _sex; }
			set { _sex = value;}
		}

		/// <summary>
		/// 用户的语言，简体中文为zh_CN 《[字段]  [varchar]》
		/// </summary>
		public string Language
		{
			get { return _language; }
			set { _language = value;}
		}

		/// <summary>
		/// 城市 《[字段]  [nvarchar]》
		/// </summary>
		public string City
		{
			get { return _city; }
			set {
				if (value != null && value.Length > 100)
					throw new ArgumentOutOfRangeException("Invalid value for City", value, value == null ? "null" : value.ToString());
				_city = value;
			}
		}

		/// <summary>
		/// 省 《[字段]  [nvarchar]》
		/// </summary>
		public string Province
		{
			get { return _province; }
			set {
				if (value != null && value.Length > 100)
					throw new ArgumentOutOfRangeException("Invalid value for Province", value, value == null ? "null" : value.ToString());
				_province = value;
			}
		}

		/// <summary>
		/// 国家 《[字段]  [nvarchar]》
		/// </summary>
		public string Country
		{
			get { return _country; }
			set {
				if (value != null && value.Length > 100)
					throw new ArgumentOutOfRangeException("Invalid value for Country", value, value == null ? "null" : value.ToString());
				_country = value;
			}
		}

		/// <summary>
		/// 用户头像，最后一个数值代表正方形头像大小（有0、46、64、96、132数值可选，0代表640*640正方形头像），用户没有头像时该项为空 《[字段]  [varchar]》
		/// </summary>
		public string Headimgurl
		{
			get { return _headimgurl; }
			set { _headimgurl = value;}
		}

		/// <summary>
		/// 用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息 《[字段]  [int]》
		/// </summary>
		public int Subscribe
		{
			get { return _subscribe; }
			set { _subscribe = value;}
		}

		/// <summary>
		/// 用户关注时间，为时间戳。如果用户曾多次关注，则取最后关注时间 《[字段]  [bigint]》
		/// </summary>
		public long? SubscribeTime
		{
			get { return _subscribeTime; }
			set { _subscribeTime = value;}
		}

		/// <summary>
		/// 导入时间 《[字段]  [datetime]》
		/// </summary>
		public DateTime Createtime
		{
			get { return _createtime; }
			set { _createtime = value;}
		}

		/// <summary>
		/// 默认构造函数
		/// </summary>
		public AppUserInfoEO() 
		{
			Openid = string.Empty;
			Subscribe = (int)0;
			Createtime = DateTime.Now;
		}

		/// <summary>
		/// 自定义构造函数
		/// </summary>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "nickname">用户的昵称</param>
		/// <param name = "sex">用户的性别，值为1时是男性，值为2时是女性，值为0时是未知</param>
		/// <param name = "language">用户的语言，简体中文为zh_CN</param>
		/// <param name = "city">城市</param>
		/// <param name = "province">省</param>
		/// <param name = "country">国家</param>
		/// <param name = "headimgurl">用户头像，最后一个数值代表正方形头像大小（有0、46、64、96、132数值可选，0代表640*640正方形头像），用户没有头像时该项为空</param>
		/// <param name = "subscribe">用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息</param>
		/// <param name = "subscribeTime">用户关注时间，为时间戳。如果用户曾多次关注，则取最后关注时间</param>
		/// <param name = "createtime">导入时间</param>
		public AppUserInfoEO(string openid, string nickname, int? sex, string language, string city, string province, string country, string headimgurl, int subscribe, long? subscribeTime, DateTime createtime) 
		{
			_openid = openid;
			_nickname = nickname;
			_sex = sex;
			_language = language;
			_city = city;
			_province = province;
			_country = country;
			_headimgurl = headimgurl;
			_subscribe = subscribe;
			_subscribeTime = subscribeTime;
			_createtime = createtime;
		}


		/// <summary>
		/// 将结果集映射成实体对象
		/// </summary>
		/// <param name = "reader">只进结果集流</param>
		/// <return></return>
		public AppUserInfoEO MapRow(IDataReader reader)
		{
			var ret = new AppUserInfoEO();
			ret.Openid = (string)reader["openid"];
			ret.OriginalOpenid = ret.Openid;
			ret.Nickname = (reader["nickname"] is DBNull) ? null : (string)reader["nickname"];
			ret.Sex = (reader["sex"] is DBNull) ? null : (int?)reader["sex"];
			ret.Language = (reader["language"] is DBNull) ? null : (string)reader["language"];
			ret.City = (reader["city"] is DBNull) ? null : (string)reader["city"];
			ret.Province = (reader["province"] is DBNull) ? null : (string)reader["province"];
			ret.Country = (reader["country"] is DBNull) ? null : (string)reader["country"];
			ret.Headimgurl = (reader["headimgurl"] is DBNull) ? null : (string)reader["headimgurl"];
			ret.Subscribe = (int)reader["subscribe"];
			ret.SubscribeTime = (reader["subscribe_time"] is DBNull) ? null : (long?)reader["subscribe_time"];
			ret.Createtime = (DateTime)reader["createtime"];
			return ret;
		}

		/// <summary>
		/// 将结果集映射成实体对象，返回映射方法
		/// </summary>
		/// <return></return>
		public Func<IDataReader, AppUserInfoEO> GetMapper()
		{
			return MapRow;
		}


	}


	#endregion
	#region MO

	/// <summary>
	/// 关注用户信息 《dbo.AppUserInfo(逻辑类)》
	/// </summary>
	public class AppUserInfoMO : DbManagerBase
	{

		#region 构造函数
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name = "routeData">路由数据</param>
		public AppUserInfoMO(object routeData = null) : base(routeData) { }

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name = "connectionStringName">连接字符串名称</param>
		public AppUserInfoMO(string connectionStringName) : base(connectionStringName) { }

		#endregion

		#region 私有方法
		/// <summary>
		/// 数据库访问操作方法
		/// </summary>
		/// <param name = "sql">SQL语句</param>
		/// <return>返回数据操作对象</return>
		private DbAdoBase AdoBase(string sql)
		{
			return DataBase.CreateDbAdo(sql, CommandType.Text);
		}

		#endregion

		#region Select方法集合
		/// <summary>
		/// 按主键查询
		/// </summary>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public AppUserInfoEO SelectByPK(string openid, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			sql.AppendFormat(@"SELECT [openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime] FROM [dbo].[AppUserInfo] WHERE [openid]=@openid ");
			return AdoBase(sql.ToString())
							.AddInParameter("@openid", openid, DbType.AnsiString, 100)
							.ExecuteReader(tm).MapToSingle(new AppUserInfoEO().GetMapper());
		}


		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<AppUserInfoEO> SelectByOpenid(string openid, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime] FROM [dbo].[AppUserInfo] WHERE [openid]=@Openid");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@Openid", openid, DbType.AnsiString, 100)
							.ExecuteList(new AppUserInfoEO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "nickname">用户的昵称</param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<AppUserInfoEO> SelectByNickname(string nickname, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime] FROM [dbo].[AppUserInfo] WHERE [nickname]=@Nickname");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@Nickname", nickname, DbType.AnsiString, 100)
							.ExecuteList(new AppUserInfoEO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "sex">用户的性别，值为1时是男性，值为2时是女性，值为0时是未知</param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<AppUserInfoEO> SelectBySex(int? sex, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime] FROM [dbo].[AppUserInfo] WHERE [sex]=@Sex");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@Sex", sex, DbType.Int32, 4)
							.ExecuteList(new AppUserInfoEO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "language">用户的语言，简体中文为zh_CN</param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<AppUserInfoEO> SelectByLanguage(string language, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime] FROM [dbo].[AppUserInfo] WHERE [language]=@Language");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@Language", language, DbType.AnsiString, 20)
							.ExecuteList(new AppUserInfoEO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "city">城市</param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<AppUserInfoEO> SelectByCity(string city, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime] FROM [dbo].[AppUserInfo] WHERE [city]=@City");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@City", city, DbType.String, 100)
							.ExecuteList(new AppUserInfoEO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "province">省</param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<AppUserInfoEO> SelectByProvince(string province, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime] FROM [dbo].[AppUserInfo] WHERE [province]=@Province");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@Province", province, DbType.String, 100)
							.ExecuteList(new AppUserInfoEO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "country">国家</param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<AppUserInfoEO> SelectByCountry(string country, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime] FROM [dbo].[AppUserInfo] WHERE [country]=@Country");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@Country", country, DbType.String, 100)
							.ExecuteList(new AppUserInfoEO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "headimgurl">用户头像，最后一个数值代表正方形头像大小（有0、46、64、96、132数值可选，0代表640*640正方形头像），用户没有头像时该项为空</param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<AppUserInfoEO> SelectByHeadimgurl(string headimgurl, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime] FROM [dbo].[AppUserInfo] WHERE [headimgurl]=@Headimgurl");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@Headimgurl", headimgurl, DbType.AnsiString, -1)
							.ExecuteList(new AppUserInfoEO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "subscribe">用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息</param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<AppUserInfoEO> SelectBySubscribe(int subscribe, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime] FROM [dbo].[AppUserInfo] WHERE [subscribe]=@Subscribe");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@Subscribe", subscribe, DbType.Int32, 4)
							.ExecuteList(new AppUserInfoEO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "subscribeTime">用户关注时间，为时间戳。如果用户曾多次关注，则取最后关注时间</param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<AppUserInfoEO> SelectBySubscribeTime(long? subscribeTime, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime] FROM [dbo].[AppUserInfo] WHERE [subscribe_time]=@SubscribeTime");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@SubscribeTime", subscribeTime, DbType.Int64, 8)
							.ExecuteList(new AppUserInfoEO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "createtime">导入时间</param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<AppUserInfoEO> SelectByCreatetime(DateTime createtime, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime] FROM [dbo].[AppUserInfo] WHERE [createtime]=@Createtime");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@Createtime", createtime, DbType.DateTime, 8)
							.ExecuteList(new AppUserInfoEO().GetMapper(), tm);
		}

		/// <summary>
		/// 按自定义条件查询
		/// </summary>
		/// <param name = "where">自定义条件,where子句</param>
		/// <param name = "top">获取行数</param>
		/// <param name = "sort">排序表达式</param>
		/// <param name = "paras">where子句中定义的参数集合</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<AppUserInfoEO> Select(string where, int top, string sort, IEnumerable<DbParameter> paras, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime] FROM [dbo].[AppUserInfo] ");
			if (!string.IsNullOrEmpty(where)) format.AppendFormat(" WHERE {0} ", where);
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddParameters(paras)
							.ExecuteList(new AppUserInfoEO().GetMapper(), tm);
		}

		/// <summary>
		/// 分页查询
		/// </summary>
		/// <param name = "where">条件表达式</param>
		/// <param name = "sort">排序表达式</param>
		/// <param name = "paras">传入参数</param>
		/// <param name = "pageSize">页大小</param>
		/// <param name = "pageIndex">页码</param>
		/// <param name = "recordCount">总记录数，由于计算总记录数影响数据库性能，只有小于或等于0的情况下才返回总记录数</param>
		/// <return>分页数据</return>
		public List<AppUserInfoEO> SelectPager(string where, string sort, IList<DbParameter> paras, long pageSize, long pageIndex, ref long recordCount)
		{
			var sql = new StringBuilder();
			sql.AppendFormat(@"SELECT [openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime] FROM [dbo].[AppUserInfo]");
			if (!string.IsNullOrEmpty(where)) sql.AppendFormat(" WHERE {0} ", where);
			if (!string.IsNullOrEmpty(sort)) sql.AppendFormat(" ORDER BY {0} ", sort);
			var pager = this.DataBase.CreatePager(sql.ToString(), pageSize, "openid");
			pager.AddParameters(paras);
			if (recordCount <= 0)
			{
				recordCount = pager.GetRecordCount();
			}
			return pager.GetPageData(pageIndex).MapToList(new AppUserInfoEO().GetMapper());
		}

		#endregion

		#region Insert方法集合
		/// <summary>
		/// 插入数据，有自增列时，item中自增字段为新增值，否则，返回影响的行数
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "item">要插入的实体对象</param>
		/// <return></return>
		public int Insert(AppUserInfoEO item, TransactionManager tm = null)
		{
			const string sql = @"INSERT INTO [dbo].[AppUserInfo] ([openid], [nickname], [sex], [language], [city], [province], [country], [headimgurl], [subscribe], [subscribe_time], [createtime]) VALUES (@openid, @nickname, @sex, @language, @city, @province, @country, @headimgurl, @subscribe, @subscribe_time, @createtime) ";
			return AdoBase(sql)
												.AddInParameter("@openid", item.Openid, DbType.AnsiString, 100)
												.AddInParameter("@nickname", item.Nickname != null ?  (object)item.Nickname : DBNull.Value, DbType.AnsiString, 100)
												.AddInParameter("@sex", item.Sex.HasValue ? (object)item.Sex.Value : DBNull.Value, DbType.Int32, 4)
												.AddInParameter("@language", item.Language != null ?  (object)item.Language : DBNull.Value, DbType.AnsiString, 20)
												.AddInParameter("@city", item.City != null ?  (object)item.City : DBNull.Value, DbType.String, 100)
												.AddInParameter("@province", item.Province != null ?  (object)item.Province : DBNull.Value, DbType.String, 100)
												.AddInParameter("@country", item.Country != null ?  (object)item.Country : DBNull.Value, DbType.String, 100)
												.AddInParameter("@headimgurl", item.Headimgurl != null ?  (object)item.Headimgurl : DBNull.Value, DbType.AnsiString, -1)
												.AddInParameter("@subscribe", item.Subscribe, DbType.Int32, 4)
												.AddInParameter("@subscribe_time", item.SubscribeTime.HasValue ? (object)item.SubscribeTime.Value : DBNull.Value, DbType.Int64, 8)
												.AddInParameter("@createtime", item.Createtime, DbType.DateTime, 8)
												.ExecuteNonQuery(tm);
		}
		#endregion

		#region Update方法集合
		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "item">要更新的实体对象</param>
		/// <param name = "tm">事务管理对象</param>
		/// <return></return>
		public int Update(AppUserInfoEO item, TransactionManager tm = null)
		{
		if (item.Openid == null) throw new ArgumentNullException("属性Openid不能为null.");
			const string sql = @"UPDATE [dbo].[AppUserInfo] SET [openid]=@openid, [nickname]=@nickname, [sex]=@sex, [language]=@language, [city]=@city, [province]=@province, [country]=@country, [headimgurl]=@headimgurl, [subscribe]=@subscribe, [subscribe_time]=@subscribe_time, [createtime]=@createtime WHERE [openid]=@OriginalOpenid";
			return AdoBase(sql)
							.AddInParameter("@OriginalOpenid", (item.HasOriginal) ? item.OriginalOpenid : item.Openid, DbType.AnsiString, 100)
							.AddInParameter("@openid", item.Openid, DbType.AnsiString, 100)
							.AddInParameter("@nickname", item.Nickname != null ?  (object)item.Nickname : DBNull.Value, DbType.AnsiString, 100)
							.AddInParameter("@sex", item.Sex.HasValue ? (object)item.Sex.Value : DBNull.Value, DbType.Int32, 4)
							.AddInParameter("@language", item.Language != null ?  (object)item.Language : DBNull.Value, DbType.AnsiString, 20)
							.AddInParameter("@city", item.City != null ?  (object)item.City : DBNull.Value, DbType.String, 100)
							.AddInParameter("@province", item.Province != null ?  (object)item.Province : DBNull.Value, DbType.String, 100)
							.AddInParameter("@country", item.Country != null ?  (object)item.Country : DBNull.Value, DbType.String, 100)
							.AddInParameter("@headimgurl", item.Headimgurl != null ?  (object)item.Headimgurl : DBNull.Value, DbType.AnsiString, -1)
							.AddInParameter("@subscribe", item.Subscribe, DbType.Int32, 4)
							.AddInParameter("@subscribe_time", item.SubscribeTime.HasValue ? (object)item.SubscribeTime.Value : DBNull.Value, DbType.Int64, 8)
							.AddInParameter("@createtime", item.Createtime, DbType.DateTime, 8)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "nickname">用户的昵称</param>
		/// <return></return>
		public virtual int UpdateNickname(string openid, string nickname)
		{
			return UpdateNickname(openid, nickname, null);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "tm">事务管理对象</param>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "nickname">用户的昵称</param>
		/// <return></return>
		public virtual int UpdateNickname(string openid, string nickname, TransactionManager tm = null)
		{
			const string sql = @"UPDATE [dbo].[AppUserInfo] SET [nickname]=@nickname  WHERE [openid]=@openid";
			return AdoBase(sql)
							.AddInParameter("@nickname", nickname != null ?  (object)nickname : DBNull.Value, DbType.AnsiString, 100)
							.AddInParameter("@openid", openid, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "sex">用户的性别，值为1时是男性，值为2时是女性，值为0时是未知</param>
		/// <return></return>
		public virtual int UpdateSex(string openid, int? sex)
		{
			return UpdateSex(openid, sex, null);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "tm">事务管理对象</param>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "sex">用户的性别，值为1时是男性，值为2时是女性，值为0时是未知</param>
		/// <return></return>
		public virtual int UpdateSex(string openid, int? sex, TransactionManager tm = null)
		{
			const string sql = @"UPDATE [dbo].[AppUserInfo] SET [sex]=@sex  WHERE [openid]=@openid";
			return AdoBase(sql)
							.AddInParameter("@sex", sex.HasValue ? (object)sex.Value : DBNull.Value, DbType.Int32, 4)
							.AddInParameter("@openid", openid, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "language">用户的语言，简体中文为zh_CN</param>
		/// <return></return>
		public virtual int UpdateLanguage(string openid, string language)
		{
			return UpdateLanguage(openid, language, null);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "tm">事务管理对象</param>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "language">用户的语言，简体中文为zh_CN</param>
		/// <return></return>
		public virtual int UpdateLanguage(string openid, string language, TransactionManager tm = null)
		{
			const string sql = @"UPDATE [dbo].[AppUserInfo] SET [language]=@language  WHERE [openid]=@openid";
			return AdoBase(sql)
							.AddInParameter("@language", language != null ?  (object)language : DBNull.Value, DbType.AnsiString, 20)
							.AddInParameter("@openid", openid, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "city">城市</param>
		/// <return></return>
		public virtual int UpdateCity(string openid, string city)
		{
			return UpdateCity(openid, city, null);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "tm">事务管理对象</param>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "city">城市</param>
		/// <return></return>
		public virtual int UpdateCity(string openid, string city, TransactionManager tm = null)
		{
			const string sql = @"UPDATE [dbo].[AppUserInfo] SET [city]=@city  WHERE [openid]=@openid";
			return AdoBase(sql)
							.AddInParameter("@city", city != null ?  (object)city : DBNull.Value, DbType.String, 100)
							.AddInParameter("@openid", openid, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "province">省</param>
		/// <return></return>
		public virtual int UpdateProvince(string openid, string province)
		{
			return UpdateProvince(openid, province, null);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "tm">事务管理对象</param>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "province">省</param>
		/// <return></return>
		public virtual int UpdateProvince(string openid, string province, TransactionManager tm = null)
		{
			const string sql = @"UPDATE [dbo].[AppUserInfo] SET [province]=@province  WHERE [openid]=@openid";
			return AdoBase(sql)
							.AddInParameter("@province", province != null ?  (object)province : DBNull.Value, DbType.String, 100)
							.AddInParameter("@openid", openid, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "country">国家</param>
		/// <return></return>
		public virtual int UpdateCountry(string openid, string country)
		{
			return UpdateCountry(openid, country, null);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "tm">事务管理对象</param>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "country">国家</param>
		/// <return></return>
		public virtual int UpdateCountry(string openid, string country, TransactionManager tm = null)
		{
			const string sql = @"UPDATE [dbo].[AppUserInfo] SET [country]=@country  WHERE [openid]=@openid";
			return AdoBase(sql)
							.AddInParameter("@country", country != null ?  (object)country : DBNull.Value, DbType.String, 100)
							.AddInParameter("@openid", openid, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "headimgurl">用户头像，最后一个数值代表正方形头像大小（有0、46、64、96、132数值可选，0代表640*640正方形头像），用户没有头像时该项为空</param>
		/// <return></return>
		public virtual int UpdateHeadimgurl(string openid, string headimgurl)
		{
			return UpdateHeadimgurl(openid, headimgurl, null);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "tm">事务管理对象</param>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "headimgurl">用户头像，最后一个数值代表正方形头像大小（有0、46、64、96、132数值可选，0代表640*640正方形头像），用户没有头像时该项为空</param>
		/// <return></return>
		public virtual int UpdateHeadimgurl(string openid, string headimgurl, TransactionManager tm = null)
		{
			const string sql = @"UPDATE [dbo].[AppUserInfo] SET [headimgurl]=@headimgurl  WHERE [openid]=@openid";
			return AdoBase(sql)
							.AddInParameter("@headimgurl", headimgurl != null ?  (object)headimgurl : DBNull.Value, DbType.AnsiString, -1)
							.AddInParameter("@openid", openid, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "subscribe">用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息</param>
		/// <return></return>
		public virtual int UpdateSubscribe(string openid, int subscribe)
		{
			return UpdateSubscribe(openid, subscribe, null);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "tm">事务管理对象</param>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "subscribe">用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息</param>
		/// <return></return>
		public virtual int UpdateSubscribe(string openid, int subscribe, TransactionManager tm = null)
		{
			const string sql = @"UPDATE [dbo].[AppUserInfo] SET [subscribe]=@subscribe  WHERE [openid]=@openid";
			return AdoBase(sql)
							.AddInParameter("@subscribe", subscribe, DbType.Int32, 4)
							.AddInParameter("@openid", openid, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "subscribeTime">用户关注时间，为时间戳。如果用户曾多次关注，则取最后关注时间</param>
		/// <return></return>
		public virtual int UpdateSubscribeTime(string openid, long? subscribeTime)
		{
			return UpdateSubscribeTime(openid, subscribeTime, null);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "tm">事务管理对象</param>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "subscribeTime">用户关注时间，为时间戳。如果用户曾多次关注，则取最后关注时间</param>
		/// <return></return>
		public virtual int UpdateSubscribeTime(string openid, long? subscribeTime, TransactionManager tm = null)
		{
			const string sql = @"UPDATE [dbo].[AppUserInfo] SET [subscribe_time]=@subscribe_time  WHERE [openid]=@openid";
			return AdoBase(sql)
							.AddInParameter("@subscribe_time", subscribeTime.HasValue ? (object)subscribeTime.Value : DBNull.Value, DbType.Int64, 8)
							.AddInParameter("@openid", openid, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "createtime">导入时间</param>
		/// <return></return>
		public virtual int UpdateCreatetime(string openid, DateTime createtime)
		{
			return UpdateCreatetime(openid, createtime, null);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name = "tm">事务管理对象</param>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		/// <param name = "createtime">导入时间</param>
		/// <return></return>
		public virtual int UpdateCreatetime(string openid, DateTime createtime, TransactionManager tm = null)
		{
			const string sql = @"UPDATE [dbo].[AppUserInfo] SET [createtime]=@createtime  WHERE [openid]=@openid";
			return AdoBase(sql)
							.AddInParameter("@createtime", createtime, DbType.DateTime, 8)
							.AddInParameter("@openid", openid, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}

		/// <summary>
		/// 按自定义条件更新数据
		/// </summary>
		/// <param name = "set">更新列</param>
		/// <param name = "where">更新条件</param>
		/// <param name = "paras">参数集合</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public int Update(string set, string where, IEnumerable<DbParameter> paras, TransactionManager tm = null)
		{
			if (string.IsNullOrEmpty(set)) throw new ArgumentNullException("set");
			var sql = string.Format(@"UPDATE [dbo].[AppUserInfo] SET {0} {1}", set, (string.IsNullOrEmpty(where) ? string.Empty : " WHERE " + where));
			return AdoBase(sql)
							.AddParameters(paras)
							.ExecuteNonQuery(tm);
		}
		#endregion

		#region Delete方法集合
		/// <summary>
		/// 按主键删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "openid">普通用户的标识，对当前公众号唯一</param>
		public int DeleteByPK(string openid, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[AppUserInfo] WHERE [openid]=@openid";
			return AdoBase(sql)
							.AddInParameter("@openid", openid, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "nickname">用户的昵称</param>
		public int DeleteByNickname(string nickname, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[AppUserInfo] WHERE [nickname]=@nickname";
			return AdoBase(sql)
							.AddInParameter("@nickname", nickname != null ?  (object)nickname : DBNull.Value, DbType.AnsiString, 100)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "sex">用户的性别，值为1时是男性，值为2时是女性，值为0时是未知</param>
		public int DeleteBySex(int? sex, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[AppUserInfo] WHERE [sex]=@sex";
			return AdoBase(sql)
							.AddInParameter("@sex", sex.HasValue ? (object)sex.Value : DBNull.Value, DbType.Int32, 4)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "language">用户的语言，简体中文为zh_CN</param>
		public int DeleteByLanguage(string language, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[AppUserInfo] WHERE [language]=@language";
			return AdoBase(sql)
							.AddInParameter("@language", language != null ?  (object)language : DBNull.Value, DbType.AnsiString, 20)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "city">城市</param>
		public int DeleteByCity(string city, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[AppUserInfo] WHERE [city]=@city";
			return AdoBase(sql)
							.AddInParameter("@city", city != null ?  (object)city : DBNull.Value, DbType.String, 100)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "province">省</param>
		public int DeleteByProvince(string province, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[AppUserInfo] WHERE [province]=@province";
			return AdoBase(sql)
							.AddInParameter("@province", province != null ?  (object)province : DBNull.Value, DbType.String, 100)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "country">国家</param>
		public int DeleteByCountry(string country, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[AppUserInfo] WHERE [country]=@country";
			return AdoBase(sql)
							.AddInParameter("@country", country != null ?  (object)country : DBNull.Value, DbType.String, 100)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "headimgurl">用户头像，最后一个数值代表正方形头像大小（有0、46、64、96、132数值可选，0代表640*640正方形头像），用户没有头像时该项为空</param>
		public int DeleteByHeadimgurl(string headimgurl, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[AppUserInfo] WHERE [headimgurl]=@headimgurl";
			return AdoBase(sql)
							.AddInParameter("@headimgurl", headimgurl != null ?  (object)headimgurl : DBNull.Value, DbType.AnsiString, -1)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "subscribe">用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息</param>
		public int DeleteBySubscribe(int subscribe, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[AppUserInfo] WHERE [subscribe]=@subscribe";
			return AdoBase(sql)
							.AddInParameter("@subscribe", subscribe, DbType.Int32, 4)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "subscribeTime">用户关注时间，为时间戳。如果用户曾多次关注，则取最后关注时间</param>
		public int DeleteBySubscribeTime(long? subscribeTime, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[AppUserInfo] WHERE [subscribe_time]=@subscribe_time";
			return AdoBase(sql)
							.AddInParameter("@subscribe_time", subscribeTime.HasValue ? (object)subscribeTime.Value : DBNull.Value, DbType.Int64, 8)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "createtime">导入时间</param>
		public int DeleteByCreatetime(DateTime createtime, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[AppUserInfo] WHERE [createtime]=@createtime";
			return AdoBase(sql)
							.AddInParameter("@createtime", createtime, DbType.DateTime, 8)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 删除指定条件的记录
		/// </summary>
		/// <param name = "whereClause">自定义删除条件，where子句。如：id=1 and name='aaa'</param>
		/// <param name = "paras">where子句中定义的参数集合</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public int Delete(string whereClause, IEnumerable<DbParameter> paras, TransactionManager tm = null)
		{
			var sql = string.Format(@"DELETE FROM [dbo].[AppUserInfo] {0}", (string.IsNullOrEmpty(whereClause) ? string.Empty : " WHERE " + whereClause));
			return AdoBase(sql)
							.AddParameters(paras)
							.ExecuteNonQuery(tm);
		}
		#endregion

		#region  自定义方法集合
		#endregion

	}


	#endregion
}
